package com.qiqijuice.sales;

import net.miginfocom.swt.MigLayout;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class OrderItemDetailsDialog {
	enum State {
		INIT,
		OK,
		CANCEL
	}
	
	private static int WIDTH = 500;
	private static int HEIGHT = 500;
	private static String TITLE = "Please speicify quantity: ";
	
	private State state;
	private Shell shell;
	private Text quantityInput;
	private Display display;
	
	private String quantity;
	
	public OrderItemDetailsDialog(Display display) {
		state = State.INIT;
		
		this.display = display;
		shell = new Shell(display, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
		
		shell.setSize(WIDTH, HEIGHT);
		shell.setText(TITLE);
		
		Rectangle r = display.getBounds();
		int x = (r.width - shell.getSize().x) / 2; 
		int y = (r.height - shell.getSize().y) / 2;
		shell.setLocation(x,y);
		
		MigLayout layout = new MigLayout("debug, wrap 2", "[75%][25%]", "[20%][80%]");
		shell.setLayout(layout);
		
		quantityInput = new Text(shell, SWT.BORDER);
		quantityInput.setLayoutData("push, grow");
		
		Button b = new Button(shell, SWT.PUSH);
		b.setLayoutData("push, grow");
		b.setText("<- Select");
		
		SelectionAdapter sa = new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				Button b = (Button) e.widget;
				quantityInput.setText(quantityInput.getText() + b.getText());
			}
		};
		
		Composite c = new Composite(shell, SWT.NONE);
		c.setLayoutData("push, grow");
		c.setLayout(new MigLayout("wrap 3",""));
		for (int i=0; i<9; ++i) {
			b = new Button(c, SWT.PUSH);
			b.setText("" + (i+1));
			b.setLayoutData("push, grow, sg 1");
			b.addSelectionListener(sa);
		}
		
		b = new Button(c, SWT.PUSH);
		b.setText("0");
		b.setLayoutData("push, grow, sg 1");
		b.addSelectionListener(sa);
		
		b = new Button(c, SWT.PUSH);
		b.setText(".");
		b.setLayoutData("push, grow, sg 1");
		b.addSelectionListener(sa);
		
		b = new Button(c, SWT.PUSH);
		b.setText("Clear");
		b.setLayoutData("push, grow, sg 1");
		b.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				quantityInput.setText("");
			}
			
		});
		
		b = new Button(shell, SWT.PUSH);
		b.setLayoutData("flowy, split 2, width 25%, ay t, height 25%, sg 1");
		b.setText("OK");
		b.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				state = State.OK;
				quantity = quantityInput.getText();
				shell.dispose();
			}
			
		});
		
		b = new Button(shell, SWT.PUSH);
		b.setLayoutData("sg 1");
		b.setText("Cancel");
		b.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				state = State.CANCEL;
				shell.dispose();
			}
			
		});
	}
	
	public boolean open() {
		state = State.CANCEL;
		shell.open();
		while(!shell.isDisposed()) {
			if (display.readAndDispatch())
				display.sleep();
		}
		
		if (state == State.OK)
			return true;
		else
			return false;
	}
	
	public float getQuantity() {
		return Float.parseFloat(quantity);
	}
	
	public static void main(String[] args) {
		Display display = new Display();
		
		OrderItemDetailsDialog d = new OrderItemDetailsDialog(display);
		d.open();
		
		display.dispose();
	}
}
